package MyApp::Controller::Taizhang;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json encode_json);
use MyApp::Model::Vpnxml;


sub get_information {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	my $db = $self->mysql->db;

	my $id = $self->param('companyId');

	my $companyinfos= $db->query('select name,address,tel from companylist where id=?',$id)->hash;

	my $macinfos = $db->query('select mac,tibaoren,issue_id from maclist where company=?',$id)->hashes;

	my $accountinfos = $db->query('select username,tibaoren,issue_id from vpn_account where company_id=?',$id)->hashes;

	$self->render(
		json => {
			companyinfos => $companyinfos,
			macinfos     => $macinfos,
			accountinfos => $accountinfos
		}
	);
}


sub get_dealing {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	##++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	my $db = $self->mysql->db;

	#my $user = "admin";    #此为测试用身份，正式系统请使用如下：
	my $user = $self->session('user');

	my ($macinfos,$accountinfos);

	$macinfos = $db->query('select * from mac_dealing')->hashes;
	$accountinfos = $db->query('select * from account_dealing')->hashes;
    
    $self->render(json => { macinfos => $macinfos,
                            accountinfos => $accountinfos });
}

sub uploadxml {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	my $db = $self->mysql->db;

	my $uploadfile = $self->param('file');

	$uploadfile->move_to('Dealfiles/import.xml');

	$self->render(json => { id => '101' });


}

sub deallist {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	my $db = $self->mysql->db;

	return $self->render(json => { success => 0 }) unless -e "Dealfiles/import.xml";

	open my $FILE,"<","Dealfiles/import.xml" or die "Can not open the file:$!\n";
    
	my $right;
	while (my $line = <$FILE>) {
		chomp $line;
		$right = 1 if $line =~ /xml\.juniper\.net/;
	}
	close $FILE;

	return $self->render( json => {success => 0} ) unless $right == 1;
	
	my $xml = MyApp::Model::Vpnxml->new("Dealfiles/import.xml");
    
	my $addmacs = $db->query('select mac from mac_dealing where operation=1')->arrays;
	my $delmacs = $db->query('select mac from mac_dealing where operation=0')->arrays;

	my $addaccount = $db->query('select username,company_name,password from account_dealing where operation=1')->hashes;
	my $delaccount = $db->query('select username from account_dealing where operation=0')->arrays;
	my $repassaccount = $db->query('select username,password from account_dealing where operation=2')->hashes;

	if ($addmacs) {
		#my $testaddmacs = $addmacs->[1];
        #open my $FILE,">>",'Dealfiles/test.txt' or die "Can't open the file: $!\n";
        #print $FILE $testaddmacs;
        #close $FILE;
		$xml->addmacaddress($addmacs);
	}

	if ($delmacs) {
		$xml->deletemacaddress($delmacs);
	}

	if ($delaccount) {
        for my $account (@$delaccount) {
			$account = $account->[0];
			$xml->deleteuser($account);
		}
	}

	if ($addaccount) {
		foreach my $hash (@$addaccount) {
			my $company_id = $db->query('select id from companylist where name=?',$hash->{company_name})->array->[0];
			my $role = $db->query('select role from sbu_company_relation where company_id=?',$company_id)->array->[0];
			my $group;

			if ($role =~ /001/) {
				$group = 'DAHAMA';
			}elsif ($role =~ /00[12]/) {
				$group = 'Dealer';
			}else {
				$group = 'supplier';
			}

			$xml->createuser($group,$hash->{username},$hash->{password},$hash->{company_name});
		}
	}

	if ($repassaccount) {
		foreach my $hash (@$repassaccount) {
			$xml->repassword($hash->{username},$hash->{password});
		}
	}

	$xml->out('Dealfiles/export.xml');

	my $mac_del_db = $db->query('select * from mac_dealing')->hashes;

	my $account_del_db = $db->query('select * from account_dealing')->hashes;

	#存储将要删除的数据库记录
	open my $HISFILE,">>","Dealfiles/History.txt" or die "Can't open the file:$!\n";

	for my $hash (@$mac_del_db) {
		print $HISFILE "$hash->{mac},$hash->{operation}\n";
		if ($hash->{operation} =~ /0/) {
			$db->query('delete from maclist where mac=?',$hash->{mac});
		}
	}
	for my $hash (@$account_del_db) {
		print $HISFILE "$hash->{username},$hash->{password},$hash->{company_name},$hash->{operation}\n";
		if ($hash->{operation} =~ /0/) {
			$db->query('delete from vpn_account where username=?',$hash->{username});
		}
	}

	close $HISFILE;

    #删除两个处理表中的数据
	$db->query('delete from mac_dealing');
	$db->query('delete from account_dealing');

    #删除公司关系表
    $db->query('delete from sbu_company_relation where enable=0');

	return $self->render(json => {success => 0}) unless -e "Dealfiles/export.xml";
	$self->render(json => {success => 1});

}

sub get_dealedxml {
	my $self = shift;

	unlink("Dealfiles/import.xml");

	$self->reply->file('Dealfiles/export.xml');
}

1;
